var http = require('http');
var url = require('url');
var util = require('util');

http.createServer(function(req, res){
    var resjson = {errno:0,errmsg:"success",data:[]};

    res.writeHead(200, {'Content-Type': 'text/json; charset=UTF-8'});

    // 解析 url 参数
    var params = url.parse(req.url, true).query;
    if(!params.url)
    {
        resjson.errmsg = "参数错误";
        resjson.errno = 1;
        res.write(JSON.stringify(resjson));
        res.end();

    }
    getData(params.url,function(data){
        resjson.data = data;
        res.write(JSON.stringify(resjson));

        res.end();


    })

}).listen(3000);


function getData(url,callback){

    http.get(url, (res) => {

        res.setEncoding('utf8');
        var rawData = '';
        res.on('data', (chunk) => rawData += chunk);
        res.on('end', () => {
            callback(rawData)
        });
    }).on('error', (e) => {
        console.log(`Got error: ${e.message}`);
    });
}